﻿using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Linq;
using WebApiV2.Data;
using WebApiV2.Data.Model;
using WebApiV2.Domain;

namespace WebApiV2.Utils
{
     public class DbInitializer
    {
        public static void Seed(IServiceProvider service)
        {
            using (var scope = service.CreateScope())
            {
                var db = scope.ServiceProvider.GetService(typeof(Admain1000DbContext)) as Admain1000DbContext;
                var _userRespository = scope.ServiceProvider.GetService(typeof(IRespository<Users>))as EfRespository<Users>;
                var _roleRespository = scope.ServiceProvider.GetService(typeof(IRespository<Roles>)) as EfRespository<Roles>;

                db.Database.EnsureCreated();

                var HasUser = db.Users.Any();

                if (!HasUser)
                {
                    var role = new Roles()
                    {
                        RolesName = "管理员"
                    };
                    _roleRespository.insert(role);
                    var user = new Users()
                    {
                        UserName = "张三",
                        PassWord = "123456",
                        Roles = role,
                    };
                    _userRespository.insert(user);

                }
            }

               

        }

      
    }
}
